package com.bonc.common.utils;

import org.apache.http.client.HttpClient;

import java.io.*;

import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.util.Map;

public class SupermanUtil {


    //公共的header头的key，与集团规范一致
    private static final String APPKEY = "appid";
    private static final String TIMESTAMP = "timestamp";
    private static final String MESSAGEID = "messageid";
    private static final String SIGN = "sign";
    //报文体的key
    private static final String OPEN_PIN = "OPEN_PIN";

    public  String sendPost(String url,Map param) {
        String result = "";
        BufferedReader in = null;
        try {
            String parameterData = "";
            for ( Object key :  param.keySet() ) {
                parameterData = parameterData + ("".equals(parameterData) ? "" : "&") + key + "=" + URLEncoder.encode(String.valueOf(param.get(key)), "UTF8");
            }

            URL realUrl = new URL(url+"?"+parameterData);
            HttpURLConnection connection = (HttpURLConnection)realUrl.openConnection();
            connection.setRequestProperty("accept", "*");
            connection.setRequestProperty("Content-Type","application/x-www-form-urlencoded;charset=utf-8");
            //connection.setRequestProperty("Authorization", "Bearer 87b9dc2482548b371d30001e280f77f6");
            connection.setRequestMethod("POST");   //设置本次请求的方式 ， 默认是GET方式， 参数要求都是大写字母
            connection.setConnectTimeout(5000);//设置连接超时
            connection.setUseCaches(false);
            connection.setDoInput(true);//是否打开输入流 ， 此方法默认为true
            connection.setDoOutput(true);//是否打开输出流， 此方法默认为false
            connection.connect();

            // 得到请求的输出流对象  执行以下代码会将上面设置的get方式修改为post方式
            OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream(),"UTF-8");
            writer.write(String.valueOf(param));
            writer.flush();

            InputStream inputStream = null;
            if ( connection.getResponseCode() >= 400) {
                inputStream = connection.getErrorStream();
            } else {
                inputStream = connection.getInputStream();
            }
            in = new BufferedReader(new InputStreamReader(inputStream,"UTF-8"));
            String line;
            while ((line = in.readLine()) != null) {
                result += line;
            }
            System.out.println("code: "+connection.getResponseCode());
            System.out.println("result: "+result);
            connection.disconnect();
        } catch (Exception e) {
            System.out.println(e);
            e.printStackTrace();
        }
        finally {
            try {
                if (in != null) {
                    in.close();
                }
            } catch (Exception e2) {
                e2.printStackTrace();
            }
        }
        return result;
    }

    public static String sendPost(String url, String param) {
        OutputStreamWriter out = null;
        BufferedReader in = null;
        String result = "";
        try {
            URL realUrl = new URL(url);
            URLConnection conn = realUrl.openConnection();
            conn.setRequestProperty("Content-Type","application/json");
            conn.setRequestProperty("connection", "keep-alive");
            conn.setRequestProperty("Authorization", "2022021611194318072e4e2f524c3da560ac5f5f812cad-6229-1642389583");
            //conn.setRequestMethod("POST");
            //conn.setUseCaches(false);
            conn.setDoOutput(true);
            conn.setDoInput(true);
            out = new OutputStreamWriter(conn.getOutputStream(), "utf-8");

            out.write(param);
            out.flush();
            in = new BufferedReader(new InputStreamReader(conn.getInputStream(),"utf-8"));
            String line;
            while ((line = in.readLine()) != null) {
                result += line;
            }
            //System.out.println("code: "+conn.getResponseCode());
            System.out.println("result: "+result);
        } catch (Exception e) {
            e.printStackTrace();
        }
        finally{
            try{
                if(out!=null){
                    out.close();
                }
                if(in!=null){
                    in.close();
                }
            }
            catch(IOException ex){
                ex.printStackTrace();
            }
        }
        return result;
    }

    public static String sendPost(String url, String token, String param) {
        OutputStreamWriter out = null;
        BufferedReader in = null;
        String result = "";
        try {
            URL realUrl = new URL(url);
            URLConnection conn = realUrl.openConnection();
            conn.setRequestProperty("Content-Type","application/json");
            conn.setRequestProperty("connection", "keep-alive");
            conn.setRequestProperty("Authorization", token);
            //conn.setRequestMethod("POST");
            //conn.setUseCaches(false);
            conn.setDoOutput(true);
            conn.setDoInput(true);
            out = new OutputStreamWriter(conn.getOutputStream(), "utf-8");

            out.write(param);
            out.flush();
            in = new BufferedReader(new InputStreamReader(conn.getInputStream(),"utf-8"));
            String line;
            while ((line = in.readLine()) != null) {
                result += line;
            }
            //System.out.println("code: "+conn.getResponseCode());
            System.out.println("result: "+result);
        } catch (Exception e) {
            e.printStackTrace();
        }
        finally{
            try{
                if(out!=null){
                    out.close();
                }
                if(in!=null){
                    in.close();
                }
            }
            catch(IOException ex){
                ex.printStackTrace();
            }
        }
        return result;
    }

}
